using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssetsManager : Singleton<AssetsManager>
{
    private Dictionary<string, Object> _assetsCache;

    protected AssetsManager()
    {
        _assetsCache = new Dictionary<string, Object>();
    }

    public virtual T GetAssets<T>(string path) where T : Object
    {
        if (_assetsCache.ContainsKey(path))
        {
            return _assetsCache[path] as T;
        }
        else
        {
            T assets = Resources.Load<T>(path);
            _assetsCache.Add(path, assets);
            return assets;
        }
    }

    /// <summary>卸载未使用的资源</summary>
    public void UnloadUnusedAssets()
    {
        Resources.UnloadUnusedAssets();
    }

    /// <summary>加载图片</summary>
    public class SpriteManager : Singleton<SpriteManager>
    {
    }

    /// <summary>加载预制体</summary>
    public class PrefabManager : Singleton<PrefabManager>
    {
        private PrefabManager()
        {
        }

        public GameObject CreateGameObjectByPrefab(string path)
        {
            GameObject prefab = AssetsManager.GetInstance().GetAssets<GameObject>(path);
            GameObject go = Object.Instantiate(prefab);
            return go;
        }

        public GameObject CreateGameObjectByPrefab(string path, Vector3 pos, Quaternion qua)
        {
            GameObject go = CreateGameObjectByPrefab(path);
            go.transform.position = pos;
            go.transform.rotation = qua;
            return go;
        }

        public GameObject CreateGameObjectByPrefab(string path, Transform parent, Vector3 localPos, Quaternion localQua)
        {
            GameObject go = CreateGameObjectByPrefab(path);
            go.transform.SetParent(parent);
            go.transform.localPosition = localPos;
            go.transform.localRotation = localQua;
            return go;
        }
    }
}